package com.frogs.ucenter.controller;

import java.util.Arrays;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.PageList;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.ApplicablePlatformEnum;
import com.frogs.system.util.CouponTypeEnum;
import com.frogs.ucenter.component.UserPermissionHelper;
import com.frogs.ucenter.entity.IndexNewCoupon;
import com.frogs.ucenter.entity.UserBaseAccountInfo;
import com.frogs.ucenter.entity.UserCoupon;
import com.frogs.ucenter.service.CouponService;

/**
 * 优惠劵
 * @author jzkangta
 *
 */
@Controller
public class CouponController extends BaseController3 {

	private static Logger log = LoggerFactory.getLogger(CouponController.class);
	
	private static final String EDIT_VIEW = "coupon/edit";
	private static final String UPDATE_VIEW = "coupon/update";
	private static final String ADD_VIEW = "coupon/add";
	private static final String LIST_VIEW = "coupon/list";
	private static final String DELETE_VIEW = "coupon/delete";
	private static final String HOME_VIEW = "coupon/index";
	
	private static final String EDIT = "/coupon/edit";
	private static final String UPDATE = "/coupon/update";
	private static final String ADD = "/coupon/add";
	private static final String LIST = "/coupon/list";
	private static final String DELETE = "/coupon/delete";
	private static final String HOME = "/coupon/index";
	private static final String NEW = "/coupon/new";
	
	@Resource
	private CouponService couponService;
	
	@Resource
	private UserPermissionHelper userPermissionHelper;
	
	@RequestMapping(HOME)
    public String home(HttpServletRequest request, ModelMap model) {
    	return HOME_VIEW;
    }
	
	@RequestMapping(LIST)
    public String list(HttpServletRequest request, ModelMap model) {
    	try {
    		HttpRequestInfo reqInfo = new HttpRequestInfo(request);
    		int status = reqInfo.getIntParameter("status", 1);//获取未使用的优惠劵 1
    		Integer pageNo = reqInfo.getIntParameter("pageNo", 1);
    		Integer pageSize = reqInfo.getIntParameter("pageSize", 8);
    		
    		Integer applicablePlatformId = 1;
    		UserBaseAccountInfo user = userPermissionHelper.getUser(request);
    		PageList<UserCoupon> list = couponService.getCouponsByUserId(pageNo, pageSize, user.getId(), applicablePlatformId,status);
    		model.addAttribute("couponStatus",status );
    		model.addAttribute("list",list );
    		model.addAttribute("pageTurn",list.getPageTurn() );
		} catch (Exception e) {
			log.error("func[home] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			
			request.setAttribute("errorMessage", e.getMessage());
			return ERROR_VIEW;
		}
    	return HOME_VIEW;
    }
	
	@RequestMapping(NEW)
    public String newCoupon(HttpServletRequest request, ModelMap model) {
    	try {
    		HttpRequestInfo reqInfo = new HttpRequestInfo(request);
    		Integer pageNo = reqInfo.getIntParameter("pageNo", 1);
    		Integer pageSize = reqInfo.getIntParameter("pageSize", 5);
    		
    		Integer applicablePlatformId = 1;
    		PageList<IndexNewCoupon> list = couponService.getNewCoupons(pageNo, pageSize, applicablePlatformId);
    		model.addAttribute("list",list );
    		model.addAttribute("pageTurn",list.getPageTurn() );
		} catch (Exception e) {
			log.error("func[newCoupon] parameterMap[{}] exception[{} - {}] desc[fail]",//
					new Object[] { request.getParameterMap(), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
			
			request.setAttribute("errorMessage", e.getMessage());
			return ERROR_VIEW;
		}
    	return HOME_VIEW;
    }
}
